#include "ClientState.h"
#include "Matrix4.h"
#include "Screen.h"
#include "EventPoller.h"
#include "Game.h"
#include "Timer.h"
#include "Client.h"
#include "NetworkEventGenerator.h"
#include <iostream>

ClientState::ClientState() :
m_camera(new Camera),
m_screenX(0),
m_screenY(0),
m_nwEG(NULL)
{
    m_clearColor.SetAll(0,0,0);
    m_cameraPosition.SetStats(30,30,30);
}
void ClientState::Update()
{
    TheClient::Instance()->Update();
    if(!TheClient::Instance()->IsConnected())
    {
        TheGame::Instance()->Quit();
    }
}
void ClientState::Draw()
{
}
void ClientState::SetUpOpenGl()
{
    //Get the screen size
    m_screenX = TheScreen::Instance()->GetWidth();
    m_screenY = TheScreen::Instance()->GetHeight();
}
void ClientState::SetUpCameras()
{
    //Set the Cameras stuff
    m_camera->SetPosition(m_cameraPosition);
    m_camera->SetLook(Vec3f(1,0,0));
}
void ClientState::Initialise()
{
#ifdef _DEBUG
    std::cout << "{AdvertState Singleton} Initialising()..." << std::endl;
#endif
    SetUpOpenGl();
    SetUpCameras();
    //Event handlers
    TheEventPoller::Instance()->AddHandler(this);

    //Set the flag
    m_initialised = true;
    m_nwEG = new NetworkEventGenerator;
    TheEventPoller::Instance()->AddHandler(m_nwEG);
    
#ifdef _DEBUG
    std::cout << "{AdvertState Singleton} Initialised Succesfully : " << std::endl;
#endif
}
void ClientState::CleanUp()
{
#ifdef _DEBUG
    std::cout << "{AdvertState Singleton} Calling CleanUp()..." << std::endl;
#endif

    if(m_nwEG)
        delete m_nwEG;
    m_nwEG = NULL;

#ifdef _DEBUG
    std::cout << "{AdvertState Singleton} CleanUp sucesfull" << std::endl;
#endif
}
void ClientState::OnResume()
{
#ifdef _DEBUG
    std::cout << "{AdvertState Singleton} Calling OnResume()..." << std::endl;
#endif

    SetUpOpenGl();
    SetUpCameras();
    /* Sets the color of the clear buffer*/
    //Event handlers
    TheEventPoller::Instance()->AddHandler(this);

#ifdef _DEBUG
    std::cout << "{AdvertState Singleton} Resume sucesfull" << std::endl;
#endif
}
void ClientState::OnKeyBoardDownEvent(const SDL_KeyboardEvent& Event)
{
    //and key press will move us into the next state
    if(Event.keysym.sym == SDLK_ESCAPE)
        TheGame::Instance()->Quit();
}
void ClientState::OnResizeEvent(const SDL_ResizeEvent& Event)
{
    float ar = (float)Event.w/Event.h;
    if(ar < 1.0f)
        ar = 1.0f;
    //store for the view ports
    m_screenX = Event.w;
    m_screenY = Event.h;
}
